﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Reflection;
using System.Runtime.InteropServices;
using Excel;
using System.IO;
using LibKetoan;

namespace Ketoan
{
	/// <summary>
	/// Summary description for Freport.
	/// </summary>
	public class frmReport : System.Windows.Forms.Form
	{
		//Language lan = new Language();
		ReportDocument oRpt;
		Excel.Application oxl;
		Excel._Workbook owb;
		Excel._Worksheet osheet;
		public bool bPrinter;
		private AccessData a;	
		private string ReportFile;
		private DataSet ds=new DataSet();
		private System.Data.DataTable dt=new System.Data.DataTable();
		private CrystalDecisions.Windows.Forms.CrystalReportViewer Report;
		private string c1="",c2="",c3="",c4="",c5="",c6="",c7="",c8="",c9="",c10="",c11="",c12="";
		private string s_Ngaythang,s_Tieude,s_Chuky1,s_Chuky2,s_Chuky3,s_Chuky4,s_ChuKy5="",s_ChuKy6="",s_ChuKy7="",s_ChuKy8="",s_ChuKy9="";
		private string s_Nam,s_TenDA,s_ChuongTrinh, s_MaSo,s_KhoiDau,s_KetThuc,s_madvsd,s_Mgs;
		private string s_Loai, s_Khoan,s_Muc,s_TenTKC2;
		private decimal d_tongkpda=0;
		private string s_Nguon,s_LoaiKP, s_NhomMuc, s_Noicap,msg;
		private bool b_baohiem=false;
		private bool b_TheoDoiDuToan=false;
		private bool b_CTChiDA=false;
		private bool b_TDNguonKP=false,bPhieu;
		private bool b_QuyetToanKP=false,b_SoNhatKyThuTien = false,bPhieunhap=false,bPhieuxuat=false;
		private System.Windows.Forms.NumericUpDown banin;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button butKetthuc;
		private System.Windows.Forms.Button butIn;
		private System.Windows.Forms.NumericUpDown den;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.NumericUpDown tu;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button butExcel;
		private System.Windows.Forms.Button butPdf;
		private string ExportPath;
		ExportOptions crExportOptions;
		DiskFileDestinationOptions crDiskFileDestinationOptions;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		#region Huu Loc
		//THEO DOI DU TOAN
		public frmReport(AccessData acc,DataSet dset,string report,string s_loaidutoan,string s_loai,string s_khoan,string s_nhommuc,string ngaybaocao,string s_ngaythangnam,string text)
		{
			InitializeComponent();
			ds=dset;a=acc;
			b_TheoDoiDuToan=true;
			s_Chuky1=s_loaidutoan;
			s_Chuky2=s_loai;
			s_Chuky3=s_khoan;
			s_Chuky4=s_nhommuc;
			s_Ngaythang= ngaybaocao;
			s_ChuKy8=s_ngaythangnam;
			s_ChuKy9=text;
			ReportFile=report;
			this.Text=report;
		}
		
		//QUYET TOAN KP
		public frmReport(AccessData acc,DataSet dset,string report,string s_loai,string s_khoan,string ngaybaocao,string s_madv,string text)
		{
			InitializeComponent();
			ds=dset;a=acc;
			b_QuyetToanKP=true;
			//s_Chuky1=s_loaidutoan;
			s_Chuky2=s_loai;
			s_Chuky3=s_khoan;
			//s_Chuky4=s_nhommuc;
			s_Ngaythang= ngaybaocao;
			s_madvsd=s_madv;
			s_Mgs=text;
			ReportFile=report;
			this.Text=report;
		}
		// THEO DOI DU TOAN
		private void PreviewReport_TheoDoiDuToan()
		{
			try
			{
				oRpt=new ReportDocument();
				oRpt.Load("..\\..\\..\\report\\"+ReportFile,OpenReportMethod.OpenReportByTempCopy);
				oRpt.SetDataSource(ds);
				oRpt.DataDefinition.FormulaFields["SoYTe"].Text="'"+a.Syte+"'";
				oRpt.DataDefinition.FormulaFields["BenhVien"].Text="'"+a.Tenbv+"'";
				oRpt.DataDefinition.FormulaFields["Ngaythang"].Text="'"+s_Ngaythang+"'";
				oRpt.DataDefinition.FormulaFields["Tieude"].Text="'"+s_Tieude+"'";
				oRpt.DataDefinition.FormulaFields["LoaiDuToan"].Text="'"+s_Chuky1+"'";
				oRpt.DataDefinition.FormulaFields["Loai"].Text="'"+s_Chuky2+"'";
				oRpt.DataDefinition.FormulaFields["Khoan"].Text="'"+s_Chuky3+"'";
				oRpt.DataDefinition.FormulaFields["NhomMuc"].Text="'"+s_Chuky4+"'";								
				oRpt.DataDefinition.FormulaFields["NguoiGhiSo"].Text="'"+s_ChuKy5+"'";
				oRpt.DataDefinition.FormulaFields["KeToanTruong"].Text="'"+s_ChuKy6+"'";
				oRpt.DataDefinition.FormulaFields["KeToanTongHop"].Text="'"+s_ChuKy7+"'";
				oRpt.DataDefinition.FormulaFields["NgayThangNam"].Text="'"+s_ChuKy8+"'";
				Report.ReportSource=oRpt;
			}
			catch (Exception e)
			{
				MessageBox.Show ("The following error was discovered: '"+e.Message+"'. It was occured in '"+e.StackTrace+"'", "Report Viewer",MessageBoxButtons.OK, MessageBoxIcon.Error);				
			}
		}	


		// QUYET TOAN KP + BAO CAO KINH PHI CHUA SU DUNG NAM TRUOC CHUYEN SANG + TANG GIAM TSCD + HOAT DONG SU NGHIEP
		private void PreviewReport_QuyetToanKP()
		{
			try
			{
				oRpt=new ReportDocument();
				oRpt.Load("..\\..\\..\\report\\"+ReportFile,OpenReportMethod.OpenReportByTempCopy);
				oRpt.SetDataSource(ds);
				oRpt.DataDefinition.FormulaFields["SoYTe"].Text="'"+a.Syte+"'";
				oRpt.DataDefinition.FormulaFields["BenhVien"].Text="'"+a.Tenbv+"'";
				oRpt.DataDefinition.FormulaFields["Ngaythang"].Text="'"+s_Ngaythang+"'";
				oRpt.DataDefinition.FormulaFields["mgs"].Text="'"+s_Mgs+"'";
				oRpt.DataDefinition.FormulaFields["Madvsdns"].Text="'"+a.Madvqhns+"'";
				oRpt.DataDefinition.FormulaFields["Text1"].Text="'"+s_Chuky2+"'";
				Report.ReportSource=oRpt;
			}
			catch (Exception e)
			{
				MessageBox.Show ("The following error was discovered: '"+e.Message+"'. It was occured in '"+e.StackTrace+"'", "Report Viewer",MessageBoxButtons.OK, MessageBoxIcon.Error);				
			}
		}		

		// BAO CAO KINH PHI CHUA SU DUNG NAM TRUOC CHUYEN SANG
		public frmReport(AccessData acc,DataSet dset,string report,string ngaybaocao,string s_madv,string text,string text1)
		{
			InitializeComponent();
			ds=dset;a=acc;
			b_QuyetToanKP=true;			
			s_Ngaythang= ngaybaocao;
			s_madvsd=s_madv;
			s_Mgs=text;
			s_Chuky2=text1;
			ReportFile=report;
			this.Text=report;
		}

		public frmReport(AccessData acc,DataSet dset,string report,string s_stk,string s_ttk,string mMsg,string ngaybaocao,string tinhchat,string doituong,string khoanmuc,string text2)
		{
			InitializeComponent();
			ds=dset;a=acc;
			b_SoNhatKyThuTien=true;
			s_Chuky1=s_stk;
			s_Chuky2=s_ttk;
			msg=mMsg;
			s_Chuky4=doituong;
			s_Ngaythang= ngaybaocao;
			s_ChuKy8=tinhchat;
			s_ChuKy5 = khoanmuc;
			s_ChuKy9=text2;
			ReportFile=report;
			this.Text=report;
		}

		private void PreviewReport_SoNhatKyThuTien()
		{
			try
			{
				oRpt=new ReportDocument();
				oRpt.Load("..\\..\\..\\report\\"+ReportFile,OpenReportMethod.OpenReportByTempCopy);
				oRpt.SetDataSource(ds);
				oRpt.DataDefinition.FormulaFields["SoYTe"].Text="'"+a.Syte+"'";
				oRpt.DataDefinition.FormulaFields["BenhVien"].Text="'"+a.Tenbv+"'";
				oRpt.DataDefinition.FormulaFields["Ngaythang"].Text="'"+s_Ngaythang+"'";
				oRpt.DataDefinition.FormulaFields["msg"].Text="'"+msg+"'";
				oRpt.DataDefinition.FormulaFields["stk"].Text="'"+s_Chuky1+"'";
				oRpt.DataDefinition.FormulaFields["ttk"].Text="'"+s_Chuky2+"'";
				oRpt.DataDefinition.FormulaFields["doituong"].Text="'"+s_Chuky4+"'";
				oRpt.DataDefinition.FormulaFields["khoanmuc"].Text="'"+s_ChuKy5+"'";
				oRpt.DataDefinition.FormulaFields["tinhchat"].Text="'"+s_ChuKy8+"'";
				Report.ReportSource=oRpt;
			}
			catch (Exception e)
			{
				MessageBox.Show ("The following error was discovered: '"+e.Message+"'. It was occured in '"+e.StackTrace+"'", "Report Viewer",MessageBoxButtons.OK, MessageBoxIcon.Error);				
			}
		}		
		#endregion Huu Loc

		public frmReport(AccessData acc,DataSet dset,string mMsg,string report)
		{
			InitializeComponent();
			ds=dset;a=acc;
			msg=mMsg;bPhieu=true;
			ReportFile=report;
			this.Text=report;
		}

		public frmReport(AccessData acc,DataSet dset,string sTungay,string sTieude,string sNguoiky,string report)
		{
			InitializeComponent();
			a=acc;
			ds=dset;
			s_Ngaythang=sTungay;
			s_Tieude=sTieude;
			s_Chuky1=sNguoiky;
			s_Chuky2="";
			s_Chuky3="";
			s_Chuky4="";
			ReportFile=report;
			this.Text=report;
		}

		public frmReport(AccessData acc,DataSet dset,string sTungay,string sTieude,string sTieude2,string sTieude3,string sTieude4,string sTieude5,string report)
		{
			InitializeComponent();
			a=acc;
			ds=dset;
			s_Ngaythang=sTungay;
			s_Tieude=sTieude;
			s_Chuky1=sTieude2;
			s_Chuky2=sTieude3;
			s_Chuky3=sTieude4;
			s_Chuky4=sTieude5;
			ReportFile=report;
			this.Text=report;
		}
		public frmReport(DataSet dset,string sTungay,string sTieude,string sTieude2,string sTieude3,string sTieude4,string sTieude5,string report)
		{
			InitializeComponent();
			//a=acc;
			ds=dset;
			s_Ngaythang=sTungay;
			s_Tieude=sTieude;
			s_Chuky1=sTieude2;
			s_Chuky2=sTieude3;
			s_Chuky3=sTieude4;
			s_Chuky4=sTieude5;
			ReportFile=report;
			this.Text=report;
		}		
		public frmReport(LibKetoan.AccessData acc,System.Data.DataTable ta,string report,string s1,string s2,string s3,string s4,string s5,string s6,string s7,string s8,string s9,string s10,string s11,string s12)
		{
			InitializeComponent();
			a=acc;dt=ta;
			c1=s1;c2=s2;c3=s3;c4=s4;c5=s5;c6=s6;
			c7=s7;c8=s8;c9=s9;c10=s10;c11=s11;c12=s12;
			ReportFile=report;this.Text=report;bPhieunhap=true;			
		}
		public frmReport(LibKetoan.AccessData acc,System.Data.DataTable ta,string report,string s1,string s2,string s3,string s4,string s5,string s6,string s7,string s8,string s9,string s10)
		{
			InitializeComponent();
			a=acc;dt=ta;
			c1=s1;c2=s2;c3=s3;c4=s4;c5=s5;c6=s6;
			c7=s7;c8=s8;c9=s9;c10=s10;
			ReportFile=report;this.Text=report;bPhieuxuat=true;			
		}
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
			try
			{
				oRpt.Dispose();

				ds.Dispose();
				dt.Dispose();
				Report.Dispose();
			}
			catch{}	;

		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmReport));
			this.Report = new CrystalDecisions.Windows.Forms.CrystalReportViewer();
			this.banin = new System.Windows.Forms.NumericUpDown();
			this.label1 = new System.Windows.Forms.Label();
			this.butKetthuc = new System.Windows.Forms.Button();
			this.butIn = new System.Windows.Forms.Button();
			this.den = new System.Windows.Forms.NumericUpDown();
			this.label3 = new System.Windows.Forms.Label();
			this.tu = new System.Windows.Forms.NumericUpDown();
			this.label2 = new System.Windows.Forms.Label();
			this.butExcel = new System.Windows.Forms.Button();
			this.butPdf = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.banin)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.den)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.tu)).BeginInit();
			this.SuspendLayout();
			// 
			// Report
			// 
			this.Report.ActiveViewIndex = -1;
			this.Report.DisplayGroupTree = false;
			this.Report.Location = new System.Drawing.Point(0, 5);
			this.Report.Name = "Report";
			this.Report.ReportSource = null;
			this.Report.Size = new System.Drawing.Size(305, 100);
			this.Report.TabIndex = 9;
			// 
			// banin
			// 
			this.banin.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.banin.Location = new System.Drawing.Point(374, 5);
			this.banin.Minimum = new System.Decimal(new int[] {
																  1,
																  0,
																  0,
																  0});
			this.banin.Name = "banin";
			this.banin.Size = new System.Drawing.Size(34, 21);
			this.banin.TabIndex = 1;
			this.banin.Value = new System.Decimal(new int[] {
																1,
																0,
																0,
																0});
			this.banin.KeyDown += new System.Windows.Forms.KeyEventHandler(this.banin_KeyDown);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(306, 5);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(65, 23);
			this.label1.TabIndex = 0;
			this.label1.Text = "&Số bản in :";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// butKetthuc
			// 
			this.butKetthuc.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.butKetthuc.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.butKetthuc.Image = ((System.Drawing.Bitmap)(resources.GetObject("butKetthuc.Image")));
			this.butKetthuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butKetthuc.Location = new System.Drawing.Point(753, 4);
			this.butKetthuc.Name = "butKetthuc";
			this.butKetthuc.Size = new System.Drawing.Size(72, 23);
			this.butKetthuc.TabIndex = 8;
			this.butKetthuc.Text = "Kết thúc";
			this.butKetthuc.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.butKetthuc.Click += new System.EventHandler(this.butKetthuc_Click);
			// 
			// butIn
			// 
			this.butIn.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.butIn.Image = ((System.Drawing.Bitmap)(resources.GetObject("butIn.Image")));
			this.butIn.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butIn.Location = new System.Drawing.Point(581, 4);
			this.butIn.Name = "butIn";
			this.butIn.Size = new System.Drawing.Size(51, 23);
			this.butIn.TabIndex = 6;
			this.butIn.Text = "      In";
			this.butIn.Click += new System.EventHandler(this.butIn_Click);
			// 
			// den
			// 
			this.den.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.den.Location = new System.Drawing.Point(535, 5);
			this.den.Name = "den";
			this.den.Size = new System.Drawing.Size(44, 21);
			this.den.TabIndex = 5;
			this.den.KeyDown += new System.Windows.Forms.KeyEventHandler(this.banin_KeyDown);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(505, 5);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(29, 23);
			this.label3.TabIndex = 4;
			this.label3.Text = "đến :";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// tu
			// 
			this.tu.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.tu.Location = new System.Drawing.Point(458, 5);
			this.tu.Name = "tu";
			this.tu.Size = new System.Drawing.Size(45, 21);
			this.tu.TabIndex = 3;
			this.tu.KeyDown += new System.Windows.Forms.KeyEventHandler(this.banin_KeyDown);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(403, 5);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(55, 23);
			this.label2.TabIndex = 2;
			this.label2.Text = "Từ trang :";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// butExcel
			// 
			this.butExcel.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.butExcel.Image = ((System.Drawing.Bitmap)(resources.GetObject("butExcel.Image")));
			this.butExcel.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butExcel.Location = new System.Drawing.Point(634, 4);
			this.butExcel.Name = "butExcel";
			this.butExcel.Size = new System.Drawing.Size(60, 23);
			this.butExcel.TabIndex = 7;
			this.butExcel.Text = "      Excel";
			this.butExcel.Click += new System.EventHandler(this.butExcel_Click);
			// 
			// butPdf
			// 
			this.butPdf.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.butPdf.Image = ((System.Drawing.Bitmap)(resources.GetObject("butPdf.Image")));
			this.butPdf.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butPdf.Location = new System.Drawing.Point(696, 4);
			this.butPdf.Name = "butPdf";
			this.butPdf.Size = new System.Drawing.Size(55, 23);
			this.butPdf.TabIndex = 13;
			this.butPdf.Text = "      PDF";
			this.butPdf.Click += new System.EventHandler(this.butPdf_Click);
			// 
			// frmReport
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.butKetthuc;
			this.ClientSize = new System.Drawing.Size(1016, 713);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.butPdf,
																		  this.butExcel,
																		  this.den,
																		  this.tu,
																		  this.banin,
																		  this.label3,
																		  this.label2,
																		  this.label1,
																		  this.butKetthuc,
																		  this.butIn,
																		  this.Report});
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.KeyPreview = true;
			this.Name = "frmReport";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Medisoft";
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
			this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmReport_KeyDown);
			this.Load += new System.EventHandler(this.frmReport_Load);
			this.Closed += new System.EventHandler(this.frmReport_Closed);
			((System.ComponentModel.ISupportInitialize)(this.banin)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.den)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.tu)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void frmReport_Load(object sender, System.EventArgs e)
		{
			this.Report.Size = new System.Drawing.Size(Screen.PrimaryScreen.WorkingArea.Width,Screen.PrimaryScreen.WorkingArea.Height);
			this.Size= new System.Drawing.Size(Screen.PrimaryScreen.WorkingArea.Width,Screen.PrimaryScreen.WorkingArea.Height);
			this.Tag=System.Environment.CurrentDirectory.ToString();
			string dir = System.IO.Directory.GetCurrentDirectory();
			ExportPath = "";
			int j = 0;
			for (int i = 0; i < dir.Length; i++)
			{
				if (dir.Substring(i, 1) == "\\") j++;
				if (j == 2) break;
				ExportPath += dir.Substring(i, 1);
			}
			ExportPath += "\\pdf\\";
			if (!System.IO.Directory.Exists(ExportPath)) System.IO.Directory.CreateDirectory(ExportPath);
			else if (bPhieunhap) Phieunhap();
			else if (bPhieuxuat) Phieuxuat();
			else if(b_CTChiDA==true)PreviewReport_CTChiDA();
			else if(b_TDNguonKP==true)PreviewReport_TDNguonKP();
			else if(b_TheoDoiDuToan==true)PreviewReport_TheoDoiDuToan();
			else if(b_QuyetToanKP==true) PreviewReport_QuyetToanKP();
			else if(b_SoNhatKyThuTien) PreviewReport_SoNhatKyThuTien();
			else if (bPhieu) PreviewReport();
			else PreviewReport_chuky();
		}

		private void Phieunhap()
		{
			try
			{
				oRpt=new ReportDocument();
				oRpt.Load("..\\..\\..\\report\\"+ReportFile,OpenReportMethod.OpenReportByTempCopy);
				oRpt.SetDataSource(dt);
				oRpt.DataDefinition.FormulaFields["soyte"].Text="'"+a.Syte+"'";
				oRpt.DataDefinition.FormulaFields["benhvien"].Text="'"+a.Tenbv+"'";
				oRpt.DataDefinition.FormulaFields["c1"].Text="'"+c1+"'";
				oRpt.DataDefinition.FormulaFields["c2"].Text="'"+c2+"'";
				oRpt.DataDefinition.FormulaFields["c3"].Text="'"+c3+"'";
				oRpt.DataDefinition.FormulaFields["c4"].Text="'"+c4+"'";
				oRpt.DataDefinition.FormulaFields["c5"].Text="'"+c5+"'";
				oRpt.DataDefinition.FormulaFields["c6"].Text="'"+c6+"'";
				oRpt.DataDefinition.FormulaFields["c7"].Text="'"+c7+"'";
				oRpt.DataDefinition.FormulaFields["c8"].Text="'"+c8+"'";
				oRpt.DataDefinition.FormulaFields["c9"].Text="'"+c9+"'";
				oRpt.DataDefinition.FormulaFields["c10"].Text="'"+c10+"'";
				oRpt.DataDefinition.FormulaFields["diachi"].Text="'"+c11+"'";
				oRpt.DataDefinition.FormulaFields["masothue"].Text="'"+c12+"'";
				oRpt.DataDefinition.FormulaFields["giamdoc"].Text="";
				oRpt.DataDefinition.FormulaFields["phutrach"].Text="";
				oRpt.DataDefinition.FormulaFields["thongke"].Text="";
				oRpt.DataDefinition.FormulaFields["ketoan"].Text="";
				oRpt.DataDefinition.FormulaFields["thukho"].Text="";
				oRpt.DataDefinition.FormulaFields["l_soluong"].Text="2";
				oRpt.DataDefinition.FormulaFields["l_dongia"].Text="2";
				oRpt.DataDefinition.FormulaFields["l_thanhtien"].Text="0";
				Report.ReportSource=oRpt;
			}
			catch (Exception e)
			{
				MessageBox.Show ("The following error was discovered: '"+e.Message+"'. It was occured in '"+e.StackTrace+"'", "Report Viewer",MessageBoxButtons.OK, MessageBoxIcon.Error);				
			}
		}

		private void Phieuxuat()
		{
			try
			{
				oRpt=new ReportDocument();
				oRpt.Load("..\\..\\..\\report\\"+ReportFile,OpenReportMethod.OpenReportByTempCopy);
				oRpt.SetDataSource(dt);
				oRpt.DataDefinition.FormulaFields["soyte"].Text="'"+a.Syte+"'";
				oRpt.DataDefinition.FormulaFields["benhvien"].Text="'"+a.Tenbv+"'";
				oRpt.DataDefinition.FormulaFields["c1"].Text="'"+c1+"'";
				oRpt.DataDefinition.FormulaFields["c2"].Text="'"+c2+"'";
				oRpt.DataDefinition.FormulaFields["c3"].Text="'"+c3+"'";
				oRpt.DataDefinition.FormulaFields["c4"].Text="'"+c4+"'";
				oRpt.DataDefinition.FormulaFields["c5"].Text="'"+c5+"'";
				oRpt.DataDefinition.FormulaFields["c6"].Text="'"+c6+"'";
				oRpt.DataDefinition.FormulaFields["c7"].Text="'"+c7+"'";
				oRpt.DataDefinition.FormulaFields["c8"].Text="'"+c8+"'";
				oRpt.DataDefinition.FormulaFields["c9"].Text="'"+c9+"'";
				oRpt.DataDefinition.FormulaFields["c10"].Text="'"+c10+"'";
				oRpt.DataDefinition.FormulaFields["giamdoc"].Text="";
				oRpt.DataDefinition.FormulaFields["phutrach"].Text="";
				oRpt.DataDefinition.FormulaFields["thongke"].Text="";
				oRpt.DataDefinition.FormulaFields["ketoan"].Text="";
				oRpt.DataDefinition.FormulaFields["thukho"].Text="";
				oRpt.DataDefinition.FormulaFields["l_soluong"].Text="2";
				oRpt.DataDefinition.FormulaFields["l_dongia"].Text="2";
				oRpt.DataDefinition.FormulaFields["l_thanhtien"].Text="0";
				Report.ReportSource=oRpt;
			}
			catch (Exception e)
			{
				MessageBox.Show ("The following error was discovered: '"+e.Message+"'. It was occured in '"+e.StackTrace+"'", "Report Viewer",MessageBoxButtons.OK, MessageBoxIcon.Error);				
			}
		}
		private void PreviewReport()
		{
			try
			{
				oRpt=new ReportDocument();
				oRpt.Load("..\\..\\..\\report\\"+ReportFile,OpenReportMethod.OpenReportByTempCopy);
				oRpt.SetDataSource(ds);				
				oRpt.DataDefinition.FormulaFields["SoYTe"].Text="'"+a.Syte+"'";
				oRpt.DataDefinition.FormulaFields["BenhVien"].Text="'"+a.Tenbv+"'";
				oRpt.DataDefinition.FormulaFields["TenBenhAn"].Text="'"+msg+"'";
				Report.ReportSource=oRpt;
			}
			catch (Exception e)
			{
				MessageBox.Show ("The following error was discovered: '"+e.Message+"'. It was occured in '"+e.StackTrace+"'", "Report Viewer",MessageBoxButtons.OK, MessageBoxIcon.Error);				
			}
		}		

		private void PreviewReport_CTChiDA()
		{
			try
			{
				oRpt=new ReportDocument();
				oRpt.Load("..\\..\\..\\report\\"+ReportFile,OpenReportMethod.OpenReportByTempCopy);
				oRpt.SetDataSource(ds);
				
				//Cac formula dung chung cho tat ca cac report
				oRpt.DataDefinition.FormulaFields["SoYTe"].Text="'"+a.Syte+"'";
				oRpt.DataDefinition.FormulaFields["BenhVien"].Text="'"+a.Tenbv+"'";
				oRpt.DataDefinition.FormulaFields["Ngaythang"].Text="'"+s_Ngaythang+"'";
				oRpt.DataDefinition.FormulaFields["Tieude"].Text="'"+s_Tieude+"'";
				oRpt.DataDefinition.FormulaFields["Text1"].Text="'"+s_Chuky1+"'";
				oRpt.DataDefinition.FormulaFields["Text2"].Text="'"+s_Chuky2+"'";
				oRpt.DataDefinition.FormulaFields["Text3"].Text="'"+s_Chuky3+"'";
				oRpt.DataDefinition.FormulaFields["Text4"].Text="'"+s_Chuky4+"'";

				oRpt.DataDefinition.FormulaFields["MaQHNS"].Text="'"+a.Madvqhns+"'";
				
				//Cac formula dung them cho Report Du An
				oRpt.DataDefinition.FormulaFields["Nam"].Text="'"+s_Nam+"'";
				oRpt.DataDefinition.FormulaFields["TenDA"].Text="'"+s_TenDA+"'";
				oRpt.DataDefinition.FormulaFields["ChuongTrinh"].Text="'"+s_ChuongTrinh+"'";
				oRpt.DataDefinition.FormulaFields["MaSo"].Text="'"+s_MaSo+"'";
				oRpt.DataDefinition.FormulaFields["KhoiDau"].Text="'"+s_KhoiDau+"'";
				oRpt.DataDefinition.FormulaFields["KetThuc"].Text="'"+s_KetThuc+"'";
				oRpt.DataDefinition.FormulaFields["Loai"].Text="'"+s_Loai+"'";
				oRpt.DataDefinition.FormulaFields["Khoan"].Text="'"+s_Khoan+"'";
				oRpt.DataDefinition.FormulaFields["Muc"].Text="'"+s_Muc+"'";
				oRpt.DataDefinition.FormulaFields["TenTKC2"].Text="'"+s_TenTKC2+"'";
				oRpt.DataDefinition.FormulaFields["TongKPDA"].Text="'"+d_tongkpda+"'";
				
				Report.ReportSource=oRpt;
			}
			catch (Exception e)
			{
				MessageBox.Show ("The following error was discovered: '"+e.Message+"'. It was occured in '"+e.StackTrace+"'", "Report Viewer",MessageBoxButtons.OK, MessageBoxIcon.Error);				
			}
		}
		private void PreviewReport_TDNguonKP()
		{
			try
			{
				oRpt=new ReportDocument();
				oRpt.Load("..\\..\\..\\report\\"+ReportFile,OpenReportMethod.OpenReportByTempCopy);
				oRpt.SetDataSource(ds);
				//ds.WriteXml("C:\\MMM.XML");
				//Cac formula dung chung cho tat ca cac report
				oRpt.DataDefinition.FormulaFields["SoYTe"].Text="'"+a.Syte+"'";
				oRpt.DataDefinition.FormulaFields["BenhVien"].Text="'"+a.Tenbv+"'";
				oRpt.DataDefinition.FormulaFields["Ngaythang"].Text="'"+s_Ngaythang+"'";
				oRpt.DataDefinition.FormulaFields["Tieude"].Text="'"+s_Tieude+"'";
				oRpt.DataDefinition.FormulaFields["Text1"].Text="'"+s_Chuky1+"'";
				oRpt.DataDefinition.FormulaFields["Text2"].Text="'"+s_Chuky2+"'";
				oRpt.DataDefinition.FormulaFields["Text3"].Text="'"+s_Chuky3+"'";
				oRpt.DataDefinition.FormulaFields["Text4"].Text="'"+s_Chuky4+"'";
				
				//Cac formula dung them cho Report TDNguonKP
				oRpt.DataDefinition.FormulaFields["Nam"].Text="'"+s_Nam+"'";
				oRpt.DataDefinition.FormulaFields["Nguon"].Text="'"+s_Nguon+"'";
				oRpt.DataDefinition.FormulaFields["LoaiKP"].Text="'"+s_LoaiKP+"'";
				oRpt.DataDefinition.FormulaFields["NoiCap"].Text="'"+s_Noicap+"'";
				oRpt.DataDefinition.FormulaFields["Loai"].Text="'"+s_Loai+"'";
				oRpt.DataDefinition.FormulaFields["Khoan"].Text="'"+s_Khoan+"'";
				
				Report.ReportSource=oRpt;
			}
			catch (Exception e)
			{
				MessageBox.Show ("The following error was discovered: '"+e.Message+"'. It was occured in '"+e.StackTrace+"'", "Report Viewer",MessageBoxButtons.OK, MessageBoxIcon.Error);				
			}
		}
		
		private void PreviewReport_chuky()
		{
			try
			{
				oRpt=new ReportDocument();
				oRpt.Load("..\\..\\..\\report\\"+ReportFile,OpenReportMethod.OpenReportByTempCopy);
				oRpt.SetDataSource(ds);
				oRpt.DataDefinition.FormulaFields["SoYTe"].Text="'"+a.Syte+"'";
				oRpt.DataDefinition.FormulaFields["BenhVien"].Text="'"+a.Tenbv+"'";
				oRpt.DataDefinition.FormulaFields["Ngaythang"].Text="'"+s_Ngaythang+"'";
				oRpt.DataDefinition.FormulaFields["Tieude"].Text="'"+s_Tieude+"'";
				oRpt.DataDefinition.FormulaFields["Text1"].Text="'"+s_Chuky1+"'";
				oRpt.DataDefinition.FormulaFields["Text2"].Text="'"+s_Chuky2+"'";
				oRpt.DataDefinition.FormulaFields["Text3"].Text="'"+s_Chuky3+"'";
				oRpt.DataDefinition.FormulaFields["Text4"].Text="'"+s_Chuky4+"'";				
//				oRpt.PrintOptions.PaperSize=PaperSize.PaperA4;
//				if (bDoc) oRpt.PrintOptions.PaperOrientation=PaperOrientation.Portrait;
//				else oRpt.PrintOptions.PaperOrientation=PaperOrientation.Landscape;
				Report.ReportSource=oRpt;
			}
			catch (Exception e)
			{
				MessageBox.Show ("The following error was discovered: '"+e.Message+"'. It was occured in '"+e.StackTrace+"'", "Report Viewer",MessageBoxButtons.OK, MessageBoxIcon.Error);				
			}
		}		

		private void frmReport_Closed(object sender, System.EventArgs e)
		{
			try
			{
				System.Environment.CurrentDirectory=this.Tag.ToString();
				this.Dispose();
				ds.Dispose();
				dt.Dispose();
				Report.Dispose();
			}
			catch{}	;
		}	


		private void butKetthuc_Click(object sender, System.EventArgs e)
		{
			bPrinter=false;
			oRpt.Close(); oRpt.Dispose();
			this.Dispose();
			this.Close();
		}

		private void butIn_Click(object sender, System.EventArgs e)
		{
			Cursor=Cursors.WaitCursor;
			bPrinter=true;
			try
			{
				oRpt.PrintToPrinter(Convert.ToInt16(banin.Value),false,Convert.ToInt16(tu.Value),Convert.ToInt16(den.Value));
				oRpt.Close(); oRpt.Dispose();
			}
			catch(Exception ex){MessageBox.Show(ex.Message);}
			Cursor=Cursors.Default;
			this.Close();
		}

		private void frmReport_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			switch (e.KeyCode)
			{
				case Keys.F5: butPdf_Click(sender, e); break;
				case Keys.F9: butIn_Click(sender,e);break;
				case Keys.F7: butExcel_Click(sender,e);break;
				case Keys.F10: butKetthuc_Click(sender,e);break;
			}
		}

		private void banin_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter) SendKeys.Send("{Tab}");
		}

		private void butExcel_Click(object sender, System.EventArgs e)
		{
			a.check_process_Excel();
			string tenfile=(b_baohiem)?a.Export_Excel(dt,ReportFile.Substring(0,ReportFile.Length-4)):a.Export_Excel(ds,ReportFile.Substring(0,ReportFile.Length-4));
			oxl=new Excel.Application();
			owb=(Excel._Workbook)(oxl.Workbooks.Open(tenfile,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value));
			osheet=(Excel._Worksheet)owb.ActiveSheet;
			oxl.ActiveWindow.DisplayGridlines=true;
			oxl.ActiveWindow.DisplayZeros=false;
			osheet.PageSetup.Orientation=XlPageOrientation.xlLandscape;
			osheet.PageSetup.PaperSize=XlPaperSize.xlPaperA4;
			osheet.PageSetup.LeftMargin=20;
			osheet.PageSetup.RightMargin=20;
			osheet.PageSetup.TopMargin=30;
			osheet.PageSetup.CenterFooter="Trang : &P/&N";
			oxl.Visible=true;
		}

		private void butPdf_Click(object sender, System.EventArgs e)
		{
			string tenfile = ReportFile.ToLower().Replace(".rpt", "");
			tenfile = ExportPath + tenfile + ".pdf";
			crDiskFileDestinationOptions = new DiskFileDestinationOptions();
			crExportOptions = oRpt.ExportOptions;
			crDiskFileDestinationOptions.DiskFileName = tenfile;
			crExportOptions.DestinationOptions = crDiskFileDestinationOptions;
			crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
			crExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
			oRpt.Export();
			try
			{
				string filerun = "AcroRd32.exe", arg = tenfile;
				if (System.IO.File.Exists(arg))
				{
					backup f = new backup(filerun, arg, true);
					f.Launch();
				}
			}
			catch
			{
				MessageBox.Show("Tập tin :" + tenfile);
			}	
		}
		#region cac ham goi report(chi)_bosung
		public frmReport(AccessData acc,DataSet dset, string sTungay,string sTieude,string sCK1,
			string sCK2,string sCK3,string sCK4,string report,		
			string sNam, string sTenDA, string sChuongTrinh, string sMaso, 
			string sKhoiDau, string sKetThuc,string sLoai, 
			string sKhoan,string sMuc, string sTenTKC2,decimal dTongkpda) 
		{
			InitializeComponent();
			b_CTChiDA=true;
			a=acc;
			ds=dset;
			s_Ngaythang=sTungay;
			s_Tieude=sTieude;
			s_Chuky1=sCK1;
			s_Chuky2=sCK2;
			s_Chuky3=sCK3;
			s_Chuky4=sCK4;
			s_Nam=sNam;
			s_TenDA=sTenDA;
			s_ChuongTrinh=sChuongTrinh;
			s_MaSo=sMaso;
			s_KhoiDau=sKhoiDau;
			s_KetThuc=sKetThuc;
			s_Loai=sLoai;
			s_Khoan=sKhoan;
			s_Muc=sMuc;
			s_TenTKC2=sTenTKC2;
			d_tongkpda=dTongkpda;
			ReportFile=report;
			this.Text=report;
		}
		public frmReport(AccessData acc,DataSet dset, string sTungay,string sTieude,string sCK1,
			string sCK2,string sCK3,string sCK4,string report,		
			string sNam,string sNguon, string sNoiCap,string sLoaiKP, 
			string sLoai,string sKhoan) 
		{
			InitializeComponent();
			b_TDNguonKP=true;
			a=acc;
			ds=dset;
			s_Ngaythang=sTungay;
			s_Tieude=sTieude;
			s_Chuky1=sCK1;
			s_Chuky2=sCK2;
			s_Chuky3=sCK3;
			s_Chuky4=sCK4;
			s_Nam=sNam;
			s_Nguon=sNguon;
			s_LoaiKP=sLoaiKP;
			s_Noicap=sNoiCap;
			s_Loai=sLoai;
			s_Khoan=sKhoan;
			ReportFile=report;
			this.Text=report;
		}
		#endregion dong ham goi report(chi)
	}
}